home *** CD-ROM | disk | FTP | other *** search
/ Inside Mac Games Volume 5 #3 / IMG 46 Vol 5-3.iso / More Goodies / More For Your Game / Realmz / Character Master Source / Nemesis Framework / Sources / nemesis resources.cpp < prev    next >
C/C++ Source or Header  |  1996-07-03  |  4KB  |  152 lines

  1. //••••••••••••••••••••••••••••••••••••••••••••••
  2. //    Some resource related functions
  3. //••••••••••••••••••••••••••••••••••••••••••••••
  4.  
  5. extern    nemesisGlobalPtr    G;
  6.     
  7. OSErr    NemesisGetPixPat( int theID, PixPatHandle &thePattern )
  8. {
  9.     OSErr    error = noErr;
  10.     // Assume failure
  11.     thePattern = nil;
  12.     thePattern = GetPixPat( theID );
  13.     
  14.     if( !thePattern )    // if nil handle
  15.         error = resNotFound;
  16.         
  17.     return error;
  18. }
  19.  
  20. void    NemesisDisposePixPat( PixPatHandle &thePattern )
  21. {
  22.     if( thePattern )    // Make sure its not nil
  23.     {
  24.         DisposePixPat( thePattern );
  25.     }
  26. }
  27.  
  28. OSErr    NemesisPenPixPat( PixPatHandle &thePattern )
  29. {
  30.     OSErr    error = noErr;
  31.     
  32.     if( thePattern ) // Check it exists
  33.     {
  34.         PenPixPat( thePattern );
  35.     }
  36.     else
  37.         error = kNilHandleError;
  38.     
  39.     return error;
  40. }
  41.     
  42. Handle    NemesisGet1Resource( short fileID, OSType resType, short resID )
  43. {
  44.     Handle        resource = nil;
  45.     short        saveResFile;
  46.     OSErr        error;
  47.     
  48.     // save the current resource file and use the one we want
  49.     saveResFile = CurResFile();
  50.     UseResFile( fileID );
  51.     error = ResError();
  52.     if (error)
  53.     {
  54.         UseResFile( saveResFile );
  55.         return resource;    // set to nil already
  56.     }    
  57.  
  58.     // try to get handle to resource from our file only
  59.     resource = Get1Resource( resType, resID );
  60.  
  61.     /*
  62.     Important Note! Some Resource Manager calls -- like Get1Resource()
  63.     will return a nil handle when they fail, but ResError() returns 
  64.     no error! You should look for BOTH an error and a nil handle. Never
  65.     trust one or the other alone.
  66.     */
  67.     error = ResError();    // look for Resource Manager error
  68.     if ( error || resource == nil )
  69.     {
  70.         resource = nil;    // make sure it is nil
  71.     }
  72.  
  73.     // restore original resource file
  74.     UseResFile(saveResFile);
  75.     return(resource);
  76. }
  77.  
  78. OSErr    NemesisGetDialog( short resID, void *dStorage, WindowRef inFront, DialogRef &theDialog)
  79. {
  80.     OSErr            error    = noErr;
  81.     DialogTHndl        dlog    = nil;
  82.     Handle            ditl    = nil;
  83.  
  84.     // assume failure right off
  85.     theDialog = nil;
  86.     
  87.     // preflight all dialog requests */
  88.     error = NemesisCheckMemory( kDialogMemory );
  89.     if (error)
  90.     {
  91.         return error;
  92.     }
  93.  
  94.     // are resources actually present?
  95.     /*
  96.     If they are not present and we call GetNewDialog(), the Mac crashes!
  97.     This might happen if some wise guy renumbered resources while
  98.     playing with ResEdit, or we forgot to create them. This approach
  99.     helps make your code as bullet proof as possible. Never assume anything!
  100.     */
  101.     dlog = (DialogTHndl)NemesisGet1Resource( G->DefaultResFile(), 'DLOG', resID );
  102.     if ( !dlog )
  103.     { 
  104.         error = resNotFound;
  105.         return error;
  106.     }
  107.     HNoPurge ( (Handle) dlog );    // don't want to lose it
  108.     
  109.     ditl = NemesisGet1Resource( G->DefaultResFile(), 'DITL', (**dlog).itemsID);
  110.     if (!ditl)
  111.     { 
  112.         HPurge ( (Handle)dlog );
  113.         error = resNotFound;
  114.         return error;
  115.     }
  116.     HNoPurge ((Handle) ditl);    // don't want to lose it
  117.  
  118.     // we're safe, get the dialog ( required resources already in memory )
  119.     theDialog = GetNewDialog( resID, dStorage, inFront );
  120.  
  121.     // always check for a nil pointer or a memory error
  122.     if ( !theDialog || MemError() )
  123.     {
  124.         HPurge ((Handle)dlog);    // clean up the other stuff
  125.         HPurge ((Handle)ditl);
  126.  
  127.         if (theDialog)
  128.         {
  129.             DisposeDialog( theDialog );
  130.             theDialog = nil;    // just to be sure
  131.         }
  132.         error = memFullErr;
  133.     }
  134.  
  135.     return(error);
  136. }
  137.  
  138. void    NemesisGetIndString( short resID, short index, Str255 myString )
  139. {
  140.     int    saveResFile;
  141.  
  142.     // save the current resource file and use the default
  143.     saveResFile = CurResFile();
  144.     UseResFile( G->DefaultResFile() );
  145.  
  146.     myString[0] = 0;    // make sure it's empty to start, in case this fails
  147.     GetIndString(myString, resID, index);
  148.     
  149.     // restore original resource file
  150.     UseResFile( saveResFile );
  151. }
  152.